<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1>键盘事件</h1>
		<ul>
			<li>keydown</li>
			<li>keyup</li>
			<li>keypress</li>
		</ul>
		<input type="text" id="input1" />
		<input type="text" id="input2" />
		<script type="text/javascript">
			input1.onkeydown = function(e){
				console.log(e.key);
				console.log("按下"+e.keyCode); // ascii
				// 0 ->48
				// 9 ->57
				if(e.keyCode!=8 &&( e.keyCode<48||e.keyCode>57))
				{
					// 阻止默认的行为
					event.preventDefault();
				}
			}
			input1.onkeyup = function(e)
			{
				console.log("抬起"+e.keyCode);
				input2.value = e.keyCode
			}
			// 可见字符
			input1.onkeypress = function(e)
			{
				console.log("按了"+e.keyCode);
			}
		</script>
	</body>
</html>
